﻿package com.tvein.sanguo.swc.action.skill
{
	//flash
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class Skill0 extends MovieClip
	{
		public static const NAME:String = "Skill-0";
		
		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var endTime:Date;
		private var skillID:int;
		private var targetID:int;
		
		public function Skill0()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			txtBeginTime.border = txtBeginTime.background = true;
			txtBeginTime.borderColor = txtBeginTime.backgroundColor = 0x333333;
			txtEndTime.border = txtEndTime.background = true;
			txtEndTime.borderColor = txtEndTime.backgroundColor = 0x333333;
			
			btnComplete.visible = false;
			
			btnCancel.addEventListener(MouseEvent.CLICK, onClick);
			btnComplete.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(400, 300);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					setData(params);
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			skillID = params[0].SkillID;
			targetID = params[0].TargetID;
			
			txtTitle.text = params[0].Title;
			txtBeginTime.text = params[0].BeginTime;
			txtEndTime.text = params[0].EndTime;
				
			var style:TextFormat = new TextFormat();
			style.align = "center";
			style.color = 0xffffff;
			style.size = 18;
				
			txtBeginTime.setTextFormat(style);
			txtEndTime.setTextFormat(style);
				
			style.font = "LiSu";
			style.color = 0xff9900;
			style.size = 30;
				
			txtTitle.setTextFormat(style);
				
			var year:int = int(txtEndTime.text.substr(0, 4));
			var month:int = int(txtEndTime.text.substr(5, 2)) - 1;
			var date:int = int(txtEndTime.text.substr(8, 2));
			var hours:int = int(txtEndTime.text.substr(11, 2));
			var minutes:int = int(txtEndTime.text.substr(14, 2));
			var seconds:int = int(txtEndTime.text.substr(17, 2));

			endTime = new Date(year, month, date, hours, minutes, seconds);
			
			var time:Timer = new Timer(1000);
 			time.addEventListener(TimerEvent.TIMER, _ontimer);
 			time.start();
		}
		
		private function _ontimer(evt:TimerEvent):void
		{
			var nowTime:Date = new Date();
			var jobTime:Number = (endTime.getTime() - nowTime.getTime()) / (1000 * 60);
			
			if (endTime > nowTime)
			{
				if (jobTime > 30)
				{
					txtRemainTime.text = "剩余时间\n（完成时间大于30分钟）\n（游戏30分钟后将会断开，请稍后回来）";
				}
				else if (jobTime > 1)
				{
					txtRemainTime.text = "剩余时间\n（距离完成时间尚有 " + Math.round(jobTime) + " 分钟）";
				}
				else
				{
					txtRemainTime.text = "剩余时间\n（距离完成时间尚有 " + Math.round(jobTime * 60) + " 秒）";
				}
			}
			else
			{
				evt.target.stop();
				evt.target.removeEventListener(TimerEvent.TIMER, _ontimer);
				
				txtRemainTime.text = "";
				btnCancel.visible = false;
				btnComplete.visible = true;
			}
		}

		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnCancel":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["SkillCall(" + skillID + ",SkillCancel," + targetID + ",);"]]));
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["RoomCall(0,RoomButton," + targetID + ",);"]]));
					break;
				case "btnComplete":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["SkillCall(" + skillID + ",SkillFinish," + targetID + ",);"]]));
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnCancel.removeEventListener(MouseEvent.CLICK, onClick);
			btnComplete.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
	}
}